<?php

use think\facade\Db;

/**
 * 授权安全码
 * @param  string $domain 域名地址
 * @return string 加密的安全码
 */
function anquancode($domain){
    $date   = date('Y-m-d');
    $code   = base64_encode(md5(base64_encode(md5($date.$date)).$domain));
    return $code;
}

/**
 * 授权域名码
 * @param  string $domain 域名地址
 * @return string 加密的安全码
 */
function domaincode($domain){
    $date   = date('Y-m');
    $code   = base64_encode(md5(base64_encode(md5($date.$date)).$domain));
    return md5($code);
}

/**
 * 版本号增加
 * @param  string $name 当前版本号
 * @return array|string 增加0.0.1之后的版本号
 */
function versions($name){
    $name = $name ?? '';
    $name  = explode('.',$name);
    $name1 = $name[0];
    $name2 = $name[1];
    $name3 = $name[2]+1;
    // 增值3
    if($name3 > 9){
        $name2 = $name2+1;
        $name3 = 1;
    }
    return $name1.'.'.$name2.'.'.$name3;
}

/**
 * 获取顶级域名
 * @param  string $domain 域名
 * @return array|string 返回顶级域名
 */
function getdomain($domain){
    $array  = explode('.',$domain);
    $count  = count($array);
    if($count == 3){
        $array = $array[0].'.';
        $domain = str_replace($array,'',$domain);
    }
    return $domain;
}

/**
 * 随机授权码
 * @param  string $domain 域名地址
 * @return string 加密的安全码
 */
function keycode($domain){
    $date   = date('Y-m-d h:i:s');
    $code   = base64_encode(md5(base64_encode(md5($date.$date)).$domain));
    return md5($code);
}

// 获取顶级域名
function getTopHost($url){
    $url   = strtolower($url);  //首先转成小写
    $hosts = parse_url($url);
    $host  = $hosts['host'];
    //查看是几级域名
    $data = explode('.', $host);
    $n = count($data);
    //判断是否是双后缀
    $preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
    if(($n > 2) && preg_match($preg,$host)){
        //双后缀取后3位
        $host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
    }else{
        //非双后缀取后两位
        $host = $data[$n-2].'.'.$data[$n-1];
    }
    return $host;
}

// 判断
function is_choice($name1,$name2){
    if(!isset($name1)){
        return false;
    }
    if(!isset($name2)){
        return false;
    }
    if($name1 != $name2){
        return false;
    }
    return true;
}

// 检测是否授权 1域名 2产品
function auth_domain($domain='',$product=''){
    $domainpd = str_replace('http://','',$domain);//get_domain($domain);
    if($domain == ''){
        return ['state'=>5,'info'=>'域名不能为空'];
    }
    // 查询授权信息
    $authorize = Db::name('auth_authorize')->where('domain',$domainpd)->find();
    // 授权是否到期
    if($authorize['auth_mode'] == 1){
        if(sj() > $authorize['expire']){
            return ['state'=>5,'info'=>'当前授权已经到期'];
        }
    }
    // 授权IP是否一致
    if($authorize['auth_mode'] == 2){
        if(uip() != $authorize['ip']){
            return ['state'=>5,'info'=>'当前授权已经到期'];
        }
    }
    // 没有授权记录盗版
    if(!$authorize){
        $daoban = Db::name('auth_daoban')->where('domain',$domain)->find();
        if(!$daoban){
            Db::name('auth_daoban')->save(['domain'=>$domain,'sj'=>sj(),'ip'=>uip(),'product'=>$product]);
        }
        return ['state'=>5,'info'=>'当前域名没有授权'];
    }
    $authorize['state'] = 1;
    return $authorize;
}

// IP地址
function uip(){
    return $_SERVER["REMOTE_ADDR"];
}

// 时间
function sj($sj=null,$day=1){
    if(!empty($sj)){
        $data = strtotime("{$day} day",strtotime($sj));
        $date = date('Y-m-d H:i:s',$data);
        return $date;
    }
    return date('Y-m-d H:i:s');
}

// 显示时间 //"Y-m-d"
function xssj($sj,$type="m-d"){
    return date($type, strtotime($sj));
}

// 限制标题输出长度
function changdu($t1,$t2='30'){
    $str=$t1;
    preg_match_all("/./us", $str, $match);
    $str_arr=$match[0];
    $length_val=count($str_arr);
    $show_str=implode('',$str_arr);
    $length_limit=$t2;
    if($length_val>$length_limit){
        $show_str="";
        for ($i=0;$i<$length_limit;$i++){
            $show_str.=$str_arr[$i];
        }
        $show_str.="...";
    }
    return $show_str;
}

function rule_url($type,$value){
    $request = request();
    $str = urldecode($request->baseUrl());
    $typename = $type;
    if(substr($str, -1)=="/"){$n="";}else{$n="/";}
    $type_name = $request->param($type);
    $name      = $n.$type."/".$type_name;//原
    $type      = $n.$type."/".$value;//新
    //查询是否存在
    if(strstr($str,$name)){
        $url=str_replace($name,$type,$str);//替换
    }else{
        $url=$str.$type;
    }
    //判断name是否为空
    if($value == null){
        $url = str_replace($typename."/","",$url);
        $url = str_replace("//","/",$url);
    }
    $url = str_replace("@",urlencode("@"),$url);
    return $url;
}

// 分页
function page($count="30",$page_size="24"){
    $page      = input('page',1);//当前页
    $count     = $count;//总数
    $page_size = $page_size;//每页显示

    $url = rule_url("page", $page);
    //总页
    if($count % $page_size){
        $page_count=(int)($count/$page_size)+1;
    } else {
        $page_count=$count/$page_size;
    }

    if($page_count>1){

        // 首页
        if($page!=1){
            $page_i    = ($page>=5)? "..." : "";
            $page_url  = rule_url("page", 1);
            $page_home = "<li class=\"oprev\"><a href=\"{$page_url}\">1 {$page_i}</a></li>";
        }else{
            $page_home = "";
        }

        $si=3;
        if($page==1){
            $si=4;
        }elseif($page==2){
            $si=3;
        }

        // 上页
        $page_upper ="";
        for($i=$page-$si;$i<$page;$i++){
            if($i>1){
                $page_url    = rule_url("page", $i);
                $page_upper .= "<li class=\"onone\"><a href=\"{$page_url}\">{$i}</a></li>";
            }
        }

        // 当前页面
        $page_middle = "<li class=\"ohave\"><a href=\"search_p1v.html\">{$page}</a></li>";

        // 下页
        $page_lower ="";
        for($i=$page+1;$i<=$page+$si;$i++){
            if($i<$page_count){
                $page_url    = rule_url("page", $i);
                $page_lower .= "<li class=\"onone\"><a href=\"{$page_url}\">{$i}</a></li>";
            }
        }
        // 尾页
        if($page<$page_count){
            $page_i    = ($i<$page_count)? "..." : "";
            $page_url  = rule_url("page", $page_count);
            $page_Last = "<li class=\"onext\"><a href=\"{$page_url}\">{$page_i} {$page_count}</a></li>";
        }else{
            $page_Last = "";
        }

        return "<div id=\"page\" total=\"{$page_count}\"><ul>{$page_home}{$page_upper}{$page_middle}{$page_lower}{$page_Last}</ul></div>";
    }
}